package org.example.springboot3.controller;

import jakarta.annotation.Resource;
import org.example.springboot3.domain.Users;
import org.example.springboot3.service.JwtUtilsService;
import org.example.springboot3.service.UsersService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin(origins = "*")//允许跨域访问
@RequestMapping("/user")
public class UserController {

   @Resource
   private UsersService usersService;
   @Resource
   private JwtUtilsService jwtUtilsService;

   //登录
   @PostMapping("/login")
   public Map<String,Object> Login(@RequestBody Map<String,Object> map) {
      Users user = usersService.getById(map.get("id").toString());
      //认证成功通过token返回用户ID和权限
      if(user!=null&&user.getPassword().equals(map.get("password").toString())) {
         Map<String,Object> result = new HashMap<>();
         result.put("UserID",user.getUserId());//获取用户ID
         result.put("UserRole",user.getRole());//获取用户权限
         String token = jwtUtilsService.createToken(result);//创建token
         result.put("Token",token);
         return result;
      }else{
         return null;
      }
   }

   //注册
   @PostMapping("/register")
   public boolean Register(@RequestBody Users user){
      if(usersService.getById(user.getUserId())==null) {
         user.setRole("普通用户");//注册用户默认设置为普通用户
         return usersService.save(user);//返回注册成功与否
      }else
         return false;
   }

   //获取个人信息
   @GetMapping("/getUserInfo")
   public Users GetById(@RequestHeader("Authorization") String token) {
      token=token.substring(7).trim();
      if(jwtUtilsService.verifyToken(token)) {//验证token是否有效
         return usersService.getUserInfo(jwtUtilsService.parseToken(token).getPayload().get("UserID").toString());//将获取的用户信息返回前端
      }
      return null;
   }

   //获取用户列表
   @GetMapping("/getUserList")
   public List<Users> GetUserList(){
      return usersService.getUserList();//返回用户列表
   }

   //修改用户信息
   @PutMapping("/updateUserInfo")
   public boolean UpdateUserInfo(@RequestHeader("Authorization") String token,@RequestBody Map<String,String> map) {
      token=token.substring(7).trim();
      if(jwtUtilsService.verifyToken(token)){//验证用户token
         String id = jwtUtilsService.parseToken(token).getPayload().get("UserID").toString();//从token中解析当前用户id
         if(map.get("info").equals("name"))//修改用户名
            return UpdateName(id,map.get("name"));
         else if(map.get("info").equals("phone"))//修改用户电话
            return UpdatePhone(id,map.get("phone"));
         else if(map.get("info").equals("pwd"))//修改用户密码
            return UpdatePwd(id,map);
         else if(map.get("info").equals("img"))
            return UpdateImg(id,map.get("img"));
         else return false;
      }else return false;
   }

   //修改用户密码
   public boolean UpdatePwd(String id,Map<String,String> pwd){
      String newPwd=pwd.get("newPwd");
      String oldPwd=pwd.get("oldPwd");
      return usersService.updatePwd(id,newPwd,oldPwd) > 0;
   }

   //修改用户名
   public boolean UpdateName(String id,String name){
      return usersService.updateName(id,name) > 0;
   }

   //修改用户电话
   public boolean UpdatePhone(String id,String phone){
      return usersService.updatePhone(id,phone) > 0;
   }

   //修改用户头像
   public boolean UpdateImg(String id,String imgPath){
      return usersService.updateImg(id,imgPath) > 0;
   }
}